#include "BinaryReader.h"

namespace opennav {

BinaryReader::BinaryReader(FileInterface* file) :
	file_(file) { }

bool BinaryReader::read(void* ptr, int32_t len) {
	int32_t read;
	return file_->read(ptr, len, read);
}

int32_t BinaryReader::read_int32() {
	int32_t result;
	read_int32(result);
	return result;
}

bool BinaryReader::read_int32(int32_t& val) {
	int32_t read;
	return file_->read(&val, sizeof(val), read);
}

int32_t BinaryReader::read_int24() {
	int32_t result;
	read_int24(result);
	return result;
}

bool BinaryReader::read_int24(int32_t& val) {
	uint8_t v1;
	int16_t v2;
	read_uint8(v1);
	read_int16(v2);
	val = v2;
	val = val << 8;
	val = val + v1;
	return true;
}

int32_t BinaryReader::read_uint24() {
	int32_t result;
	read_uint24(result);
	return result;
}

bool BinaryReader::read_uint24(int32_t& val) {
	uint8_t v1;
	uint16_t v2;
	read_uint8(v1);
	read_uint16(v2);
	val = v2;
	val = val << 8;
	val = val + v1;
	return true;
}

int16_t BinaryReader::read_int16() {
	int16_t result;
	read_int16(result);
	return result;
}

bool BinaryReader::read_int16(int16_t& val) {
	int32_t read;
	return file_->read(&val, sizeof(val), read);
}

int8_t BinaryReader::read_int8() {
	int8_t result;
	read_int8(result);
	return result;
}

bool BinaryReader::read_int8(int8_t& val) {
	int32_t read;
	return file_->read(&val, sizeof(val), read);
}

uint32_t BinaryReader::read_uint32() {
	uint32_t result;
	read_uint32(result);
	return result;
}

bool BinaryReader::read_uint32(uint32_t& val) {
	int32_t read;
	return file_->read(&val, sizeof(val), read);
}

uint16_t BinaryReader::read_uint16() {
	uint16_t result;
	read_uint16(result);
	return result;
}

bool BinaryReader::read_uint16(uint16_t& val) {
	int32_t read;
	return file_->read(&val, sizeof(val), read);
}

uint8_t BinaryReader::read_uint8() {
	uint8_t result;
	read_uint8(result);
	return result;
}

bool BinaryReader::read_uint8(uint8_t& val) {
	int32_t read;
	return file_->read(&val, sizeof(val), read);
}

}
